﻿using HslCommunication;
using HslCommunication.Profinet.Siemens;
using log4net;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YnPlcDataServer.Entities;
using YnPlcDataServer.Utils;
using YnPlcDataServer.Variable;

namespace YnPlcDataServer.Scheduler
{
    public class GetDataJob : IJob
    {
        #region 变量定义
        private static ILog quartzLog = LogManager.GetLogger(typeof(GetDataJob));
        private SiemensS7Net siemensS7Net = null;
        private SiemensPPI siemensPPI = null;
        private static List<YnSvData> listYnSvData_last = new List<YnSvData>();
        #endregion

        public static void init()
        {
            listYnSvData_last.Clear();
        }
        /// <summary>
        /// 作业调度每次定时执行方法
        /// </summary>
        /// <param name="context"></param>
        public async Task Execute(IJobExecutionContext context)
        {
            await Task.Run(() =>
            {
                //Console.WriteLine($"hello! 当前时间：{DateTime.Now}");
                //Console.WriteLine($"触发时间：{context.ScheduledFireTimeUtc?.LocalDateTime},下次触发时间：{context.NextFireTimeUtc?.LocalDateTime}");
                //Console.WriteLine();
                try
                {
                    JobKey jobKey = context.JobDetail.Key;
                    JobDataMap dataMap = context.JobDetail.JobDataMap;
                    int daq_id = dataMap.GetInt("daq_id");
                    if (PublicVariable.ynDaq != null)
                    {
                        Communication.ProtocolType protocolType = Communication.ProtocolType.SiemensPPI;
                        try
                        {
                            protocolType = (Communication.ProtocolType)Enum.Parse(typeof(Communication.ProtocolType), PublicVariable.ynDaq.protocol_type);
                        }
                        catch (Exception) { }

                        HlsConnect(protocolType);

                        //为了引入存储规则，改变存储方式，改为批量存储
                        //List<YnSvData> listYnSvData_last = new List<YnSvData>();
                        List<YnSvData> listYnSvData = new List<YnSvData>();
                        #region read
                        //foreach (YnSvid ynSvid in PublicVariable.listYnSvid)
                        for (int i = 0; i < PublicVariable.listYnSvid.Count;i++ )
                        {
                            YnSvid ynSvid = PublicVariable.listYnSvid[i];
                            try
                            {
                                DataType dataType = DataType.None;
                                try
                                {
                                    dataType = (DataType)Enum.Parse(typeof(DataType), ynSvid.date_type);
                                }
                                catch (Exception) { }


                                if (dataType == DataType.Coil)
                                {
                                    #region DataType.bool
                                    //HslCommunication.OperateResult<bool> result = null;
                                    //if (protocolType == Communication.ProtocolType.SiemensPPI)
                                    //{
                                    //    result = siemensPPI.ReadCoil(ynSvid.address);
                                    //}
                                    //else if (protocolType == Communication.ProtocolType.SiemensS7Net_1200)
                                    //{
                                    //    result = siemensS7Net.ReadCoil(ynSvid.address);
                                    //}
                                    //HslCommunication.OperateResult<bool> result = busTcpClient.ReadCoil(myAddress.address);
                                    //if (result.IsSuccess)
                                    //{
                                    //    YnCommServer.Database.DataFile.WriteData(myAddress.address, result.Content.ToString());
                                    //    //textBox.AppendText( DateTime.Now.ToString( "[HH:mm:ss] " ) + $"[{address}] {result.Content}{Environment.NewLine}" );
                                    //}
                                    //else
                                    //{
                                    //    //MessageBox.Show( DateTime.Now.ToString( "[HH:mm:ss] " ) + $"[{address}] 读取失败{Environment.NewLine}原因：{result.ToMessageShowString( )}" );
                                    //    throw new Exception("[" + myAddress.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                    //}
                                    #endregion
                                }
                                else if (dataType == DataType.Discrete)
                                {
                                    #region DataType.bool
                                    //HslCommunication.OperateResult<bool> result = null;
                                    //if (protocolType == Communication.ProtocolType.SiemensPPI)
                                    //{
                                    //    result = siemensPPI.ReadDiscrete(ynSvid.address);
                                    //}
                                    //else if (protocolType == Communication.ProtocolType.SiemensS7Net_1200)
                                    //{
                                    //    result = siemensS7Net.ReadDiscrete(ynSvid.address);
                                    //}
                                    //if (result.IsSuccess)
                                    //{
                                    //    //写入数据库
                                    //    SQLiteHelper.AddHistoryData(ynSvid.svid_id, result.Content.ToString());
                                    //}
                                    //else
                                    //{
                                    //    //throw new Exception("[" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                    //    quartzLog.Error("[" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                    //    MyLogger.Info("[" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                    //}
                                    #endregion
                                }
                                else if (dataType == DataType.Bool)
                                {
                                    #region DataType.Bool
                                    HslCommunication.OperateResult<bool> result = null;
                                    if (protocolType == Communication.ProtocolType.SiemensPPI)
                                    {
                                        result = siemensPPI.ReadBool(ynSvid.address);
                                    }
                                    else if (protocolType == Communication.ProtocolType.SiemensS7Net_1200
                                        || protocolType == Communication.ProtocolType.SiemensS7Net_1500)
                                    {
                                        result = siemensS7Net.ReadBool(ynSvid.address);
                                    }
                                    if (result.IsSuccess)
                                    {
                                        //写入数据库
                                        //SQLiteHelper.AddHistoryData(ynSvid.svid_id, result.Content.ToString());
                                        listYnSvData.Add(new YnSvData(ynSvid.svid_id, result.Content.ToString()));
                                    }
                                    else
                                    {
                                        //throw new Exception("[" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        quartzLog.Error("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        MyLogger.Info("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                    }
                                    #endregion
                                }
                                else if (dataType == DataType.Short)
                                {
                                    #region DataType.Int
                                    HslCommunication.OperateResult<short> result = null;
                                    if (protocolType == Communication.ProtocolType.SiemensPPI)
                                    {
                                        result = siemensPPI.ReadInt16(ynSvid.address);
                                    }
                                    else if (protocolType == Communication.ProtocolType.SiemensS7Net_1200
                                        || protocolType == Communication.ProtocolType.SiemensS7Net_1500)
                                    {
                                        result = siemensS7Net.ReadInt16(ynSvid.address);
                                    }
                                    if (result.IsSuccess)
                                    {
                                        //写入数据库
                                        //SQLiteHelper.AddHistoryData(ynSvid.svid_id, result.Content.ToString());
                                        listYnSvData.Add(new YnSvData(ynSvid.svid_id, result.Content.ToString()));
                                    }
                                    else
                                    {
                                        //throw new Exception("[" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        quartzLog.Error("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        MyLogger.Info("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                    }
                                    #endregion
                                }
                                else if (dataType == DataType.Ushort)
                                {
                                    #region DataType.Int
                                    HslCommunication.OperateResult<ushort> result = null;
                                    if (protocolType == Communication.ProtocolType.SiemensPPI)
                                    {
                                        result = siemensPPI.ReadUInt16(ynSvid.address);
                                    }
                                    else if (protocolType == Communication.ProtocolType.SiemensS7Net_1200
                                        || protocolType == Communication.ProtocolType.SiemensS7Net_1500)
                                    {
                                        result = siemensS7Net.ReadUInt16(ynSvid.address);
                                    }
                                    if (result.IsSuccess)
                                    {
                                        //写入数据库
                                        //SQLiteHelper.AddHistoryData(ynSvid.svid_id, result.Content.ToString());
                                        listYnSvData.Add(new YnSvData(ynSvid.svid_id, result.Content.ToString()));
                                    }
                                    else
                                    {
                                        //throw new Exception("[" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        quartzLog.Error("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        MyLogger.Info("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                    }
                                    #endregion
                                }
                                else if (dataType == DataType.Int)
                                {
                                    #region DataType.Int
                                    HslCommunication.OperateResult<int> result = null;
                                    if (protocolType == Communication.ProtocolType.SiemensPPI)
                                    {
                                        result = siemensPPI.ReadInt32(ynSvid.address);
                                    }
                                    else if (protocolType == Communication.ProtocolType.SiemensS7Net_1200
                                        || protocolType == Communication.ProtocolType.SiemensS7Net_1500)
                                    {
                                        result = siemensS7Net.ReadInt32(ynSvid.address);
                                    }
                                    if (result.IsSuccess)
                                    {
                                        //写入数据库
                                        //SQLiteHelper.AddHistoryData(ynSvid.svid_id, result.Content.ToString());
                                        listYnSvData.Add(new YnSvData(ynSvid.svid_id, result.Content.ToString()));
                                    }
                                    else
                                    {
                                        //throw new Exception("[" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        quartzLog.Error("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        MyLogger.Info("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                    }
                                    #endregion
                                }
                                else if (dataType == DataType.UInt)
                                {
                                    #region DataType.Int
                                    HslCommunication.OperateResult<uint> result = null;
                                    if (protocolType == Communication.ProtocolType.SiemensPPI)
                                    {
                                        result = siemensPPI.ReadUInt32(ynSvid.address);
                                    }
                                    else if (protocolType == Communication.ProtocolType.SiemensS7Net_1200
                                        || protocolType == Communication.ProtocolType.SiemensS7Net_1500)
                                    {
                                        result = siemensS7Net.ReadUInt32(ynSvid.address);
                                    }
                                    if (result.IsSuccess)
                                    {
                                        //写入数据库
                                        //SQLiteHelper.AddHistoryData(ynSvid.svid_id, result.Content.ToString());
                                        listYnSvData.Add(new YnSvData(ynSvid.svid_id, result.Content.ToString()));
                                    }
                                    else
                                    {
                                        //throw new Exception("[" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        quartzLog.Error("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        MyLogger.Info("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                    }
                                    #endregion
                                }
                                else if (dataType == DataType.Long)
                                {
                                    #region DataType.Int
                                    HslCommunication.OperateResult<long> result = null;
                                    if (protocolType == Communication.ProtocolType.SiemensPPI)
                                    {
                                        result = siemensPPI.ReadInt64(ynSvid.address);
                                    }
                                    else if (protocolType == Communication.ProtocolType.SiemensS7Net_1200
                                        || protocolType == Communication.ProtocolType.SiemensS7Net_1500)
                                    {
                                        result = siemensS7Net.ReadInt64(ynSvid.address);
                                    }
                                    if (result.IsSuccess)
                                    {
                                        //写入数据库
                                        //SQLiteHelper.AddHistoryData(ynSvid.svid_id, result.Content.ToString());
                                        listYnSvData.Add(new YnSvData(ynSvid.svid_id, result.Content.ToString()));
                                    }
                                    else
                                    {
                                        //throw new Exception("[" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        quartzLog.Error("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        MyLogger.Info("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                    }
                                    #endregion
                                }
                                else if (dataType == DataType.ULong)
                                {
                                    #region DataType.Int
                                    HslCommunication.OperateResult<ulong> result = null;
                                    if (protocolType == Communication.ProtocolType.SiemensPPI)
                                    {
                                        result = siemensPPI.ReadUInt64(ynSvid.address);
                                    }
                                    else if (protocolType == Communication.ProtocolType.SiemensS7Net_1200
                                        || protocolType == Communication.ProtocolType.SiemensS7Net_1500)
                                    {
                                        result = siemensS7Net.ReadUInt64(ynSvid.address);
                                    }
                                    if (result.IsSuccess)
                                    {
                                        //写入数据库
                                        //SQLiteHelper.AddHistoryData(ynSvid.svid_id, result.Content.ToString());
                                        listYnSvData.Add(new YnSvData(ynSvid.svid_id, result.Content.ToString()));
                                    }
                                    else
                                    {
                                        //throw new Exception("[" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        quartzLog.Error("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        MyLogger.Info("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                    }
                                    #endregion
                                }
                                else if (dataType == DataType.Double)
                                {
                                    #region DataType.Int
                                    HslCommunication.OperateResult<Double> result = null;
                                    if (protocolType == Communication.ProtocolType.SiemensPPI)
                                    {
                                        result = siemensPPI.ReadDouble(ynSvid.address);
                                    }
                                    else if (protocolType == Communication.ProtocolType.SiemensS7Net_1200
                                        || protocolType == Communication.ProtocolType.SiemensS7Net_1500)
                                    {
                                        result = siemensS7Net.ReadDouble(ynSvid.address);
                                    }
                                    if (result.IsSuccess)
                                    {
                                        //写入数据库
                                        //SQLiteHelper.AddHistoryData(ynSvid.svid_id, result.Content.ToString("0.00000"));
                                        listYnSvData.Add(new YnSvData(ynSvid.svid_id, result.Content.ToString("0.00000")));
                                    }
                                    else
                                    {
                                        //throw new Exception("[" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        quartzLog.Error("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        MyLogger.Info("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                    }
                                    #endregion
                                }
                                else if (dataType == DataType.Float)
                                {
                                    #region DataType.Int
                                    HslCommunication.OperateResult<float> result = null;
                                    if (protocolType == Communication.ProtocolType.SiemensPPI)
                                    {
                                        result = siemensPPI.ReadFloat(ynSvid.address);
                                    }
                                    else if (protocolType == Communication.ProtocolType.SiemensS7Net_1200
                                        || protocolType == Communication.ProtocolType.SiemensS7Net_1500)
                                    {
                                        result = siemensS7Net.ReadFloat(ynSvid.address);
                                    }
                                    if (result.IsSuccess)
                                    {
                                        //写入数据库
                                        //SQLiteHelper.AddHistoryData(ynSvid.svid_id, result.Content.ToString("0.00000"));
                                        listYnSvData.Add(new YnSvData(ynSvid.svid_id, result.Content.ToString("0.00000")));
                                    }
                                    else
                                    {
                                        //throw new Exception("[" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        quartzLog.Error("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        MyLogger.Info("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                    }
                                    #endregion
                                }
                                else if (dataType == DataType.String)
                                {
                                    #region DataType.Int
                                    HslCommunication.OperateResult<string> result = null;
                                    if (protocolType == Communication.ProtocolType.SiemensPPI)
                                    {
                                        //result = siemensPPI.ReadString(ynSvid.address);
                                    }
                                    else if (protocolType == Communication.ProtocolType.SiemensS7Net_1200
                                        || protocolType == Communication.ProtocolType.SiemensS7Net_1500)
                                    {
                                        result = siemensS7Net.ReadString(ynSvid.address, (ushort)ynSvid.length);
                                    }
                                    if (result.IsSuccess)
                                    {
                                        //写入数据库
                                        //SQLiteHelper.AddHistoryData(ynSvid.svid_id, result.Content.ToString());
                                        listYnSvData.Add(new YnSvData(ynSvid.svid_id, result.Content.ToString()));
                                    }
                                    else
                                    {
                                        //throw new Exception("[" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        quartzLog.Error("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                        MyLogger.Info("[" + ynSvid.name + ":" + ynSvid.address + "] 读取失败" + Environment.NewLine + "原因：" + result.ToMessageShowString());
                                    }
                                    #endregion
                                }
                            }
                            catch (Exception ex)
                            {
                                quartzLog.Error(ex.Message);
                                MyLogger.Info(ex.Message);
                            }

                        }
                        #endregion

                        #region save db
                        bool canSaveDb = true;
                        if (PublicVariable.listYnSectionRule != null && PublicVariable.listYnSectionRule.Count > 0)
                        {
                            foreach (YnSectionRule ynSectionRule in PublicVariable.listYnSectionRule)
                            {
                                //MyLogger.Info("规则》" + ynSectionRule.rule_id + "-->" + ynSectionRule.enabled);
                                if (!ynSectionRule.enabled)
                                {
                                    //MyLogger.Info("continue....");
                                    continue;
                                }
                                if (ynSectionRule.type == SQLiteHelper.SECTION_RULE_TYPE_Condition)
                                {
                                    //MyLogger.Info("有条件采集规则");
                                    //条件规则
                                    foreach (YnSvData ynSvData in listYnSvData)
                                    {
                                        if (ynSvData.svid_id == ynSectionRule.svid_id)
                                        {
                                            decimal _value = 0;
                                            decimal.TryParse(ynSvData.value, out _value);
                                            if (!(_value >= ynSectionRule.condition_lower_limit && _value <= ynSectionRule.condition_upper_limit))
                                            {
                                                canSaveDb = false;
                                                //MyLogger.Info("采集：Condition：" + ynSvData.svid_id + ":" + ynSvData.value + ":lower:" + ynSectionRule.condition_lower_limit.ToString() + ":upper:" + ynSectionRule.condition_upper_limit.ToString());
                                            }
                                            break;
                                        }
                                    }
                                }
                                else if (ynSectionRule.type == SQLiteHelper.SECTION_RULE_TYPE_Trigger)
                                {
                                    //MyLogger.Info("有触发采集规则");
                                    //触发规则
                                    foreach (YnSvData ynSvData in listYnSvData)
                                    {
                                        if (ynSvData.svid_id == ynSectionRule.svid_id)
                                        {
                                            YnSvData ynSvData_last = listYnSvData_last.Find(item => item.svid_id == ynSvData.svid_id);
                                            //MyLogger.Info("listYnSvData_last count：" + listYnSvData_last.Count + ":ynSvData.svid_id==" + ynSvData.svid_id.ToString() + ":ynSvData_last=" + ((ynSvData_last == null) ? "null" : ynSvData_last.svid_id.ToString()));
                                            if (ynSvData_last != null)
                                            {
                                                decimal _value = 0;
                                                decimal.TryParse(ynSvData.value, out _value);
                                                decimal _value_last = 0;
                                                decimal.TryParse(ynSvData_last.value, out _value_last);
                                                //如果前后值小于触发范围，则不采集
                                                //MyLogger.Info("采集：触发：" + ynSvData.svid_id + ":上次" + _value_last.ToString() + ":本次" + _value.ToString() + ":trigger_changed:" + ynSectionRule.trigger_changed.ToString());
                                                if ((Math.Abs(_value_last - _value) < ynSectionRule.trigger_changed))
                                                {
                                                    canSaveDb = false;
                                                }
                                            }
                                            break;
                                        }
                                    }
                                }
                                if (!canSaveDb)
                                {
                                    break;
                                }
                            }
                        }
                        if (canSaveDb)
                        {
                            //MyLogger.Info("写入数据");
                            //内存保留最后一个存储断面数据
                            DateTime dtCurrent = DateTime.Now;
                            listYnSvData_last.Clear();
                            listYnSvData_last.AddRange(listYnSvData);
                            foreach (YnSvData ynSvData in listYnSvData)
                            {
                                try
                                {
                                    //写入数据库
                                    SQLiteHelper.AddHistoryData(ynSvData.svid_id, ynSvData.value, dtCurrent);
                                }
                                catch (Exception ex)
                                {
                                    quartzLog.Error(ex.Message);
                                    MyLogger.Info(ex.Message);
                                }
                            }
                        }
                        #endregion


                        HlsConnectClose(protocolType);
                    }
                }
                catch (Exception ex)
                {
                    quartzLog.Error(ex.Message);
                    MyLogger.Info(ex.Message);
                }

            });
            //await Console.Out.WriteLineAsync("Greetings from HelloJob!");
        }
        private void HlsConnect(Communication.ProtocolType protocolType)
        {
            try
            {
                if (protocolType == Communication.ProtocolType.SiemensPPI)
                {
                    #region SiemensPPI
                    siemensPPI = new SiemensPPI();
                    siemensPPI.SerialPortInni(sp =>
                    {
                        sp.PortName = PublicVariable.ynDaq.com_port;
                        sp.BaudRate = PublicVariable.ynDaq.com_baudrate;
                        sp.DataBits = PublicVariable.ynDaq.com_databits;
                        sp.StopBits = PublicVariable.ynDaq.com_stopbits == 0 ? System.IO.Ports.StopBits.None : (PublicVariable.ynDaq.com_stopbits == 1 ? System.IO.Ports.StopBits.One : System.IO.Ports.StopBits.Two);
                        sp.Parity = PublicVariable.ynDaq.com_parity == "无" ? System.IO.Ports.Parity.None : (PublicVariable.ynDaq.com_parity == "奇" ? System.IO.Ports.Parity.Odd : System.IO.Ports.Parity.Even);
                    });
                    siemensPPI.Open();
                    siemensPPI.Station = (byte)PublicVariable.ynDaq.com_station;
                    #endregion
                }
                else if (protocolType == Communication.ProtocolType.SiemensS7Net_1200
                    || protocolType == Communication.ProtocolType.SiemensS7Net_1500)
                {
                    #region SiemensS7Net_1200、SiemensS7Net_1500
                    System.Net.IPAddress address;
                    if (!System.Net.IPAddress.TryParse(PublicVariable.ynDaq.net_ip, out address))
                    {
                        throw new Exception("Ip地址不正确！");
                    }
                    if (protocolType == Communication.ProtocolType.SiemensS7Net_1200)
                        siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200);
                    else if (protocolType == Communication.ProtocolType.SiemensS7Net_1500)
                        siemensS7Net = new SiemensS7Net(SiemensPLCS.S1500);
                    siemensS7Net.IpAddress = address.ToString();
                    siemensS7Net.Port = 102;//?固定
                    siemensS7Net.Rack = (byte)PublicVariable.ynDaq.net_rack;
                    siemensS7Net.Slot = (byte)PublicVariable.ynDaq.net_slot;

                    OperateResult connect = siemensS7Net.ConnectServer();
                    if (!connect.IsSuccess)
                    {
                        throw new Exception("Ip连接失败：" + HslCommunication.StringResources.Language.ConnectedFailed);
                    }
                    //userControlCurve1.ReadWriteNet = siemensTcpNet;
                    #endregion
                }
                
            }
            catch (Exception ex)
            {
                //quartzLog.Error(ex.Message);
                //MyLogger.Info(ex.Message);
                throw ex;
            }

        }
        private void HlsConnectClose(Communication.ProtocolType protocolType)
        {
            try
            {
                // 断开连接
                if (protocolType == Communication.ProtocolType.SiemensPPI)
                {
                    siemensPPI.Close();

                }
                else if (protocolType == Communication.ProtocolType.SiemensS7Net_1200
                    || protocolType == Communication.ProtocolType.SiemensS7Net_1500
                    )
                {
                    siemensS7Net.ConnectClose();
                }
            }
            catch (Exception ex)
            {
                //quartzLog.Error(ex.Message);
                //MyLogger.Info(ex.Message);
                throw ex;
            }

        }
    }
}
